import * as THREE from '../../build/three.module.js';
import {
  OrbitControls
} from '../../jsm/controls/OrbitControls.js';

const scene = new THREE.Scene();

// 蓝色长方体网格模型，作为投影对象
const blueBoxGeo = new THREE.BoxGeometry(50, 75, 50);
const blueBoxMat = new THREE.MeshLambertMaterial({
  color: 0x0000ff
});
const meshBox = new THREE.Mesh(blueBoxGeo, blueBoxMat);
scene.add(meshBox);
// 开启投影
meshBox.castShadow = true; // 允许该网格模型响应光照产生投影

// 灰色的平面模型，作为投影面
const planeGeo = new THREE.PlaneGeometry(300, 200);
const planeMat = new THREE.MeshLambertMaterial({
  color: 0x999999,
  side: THREE.DoubleSide
});
const meshPlane = new THREE.Mesh(planeGeo, planeMat);
scene.add(meshPlane);
meshPlane.rotateX(-90 * (Math.PI / 180));
meshPlane.position.y = -50;
// 开启投影
meshPlane.receiveShadow = true; // 允许该模型接受其他模型投影混合

// 环境光
// const abL = new THREE.AmbientLight(0x333333, 1);
// scene.add(abL);

// 平行光
// const directionalL = new THREE.DirectionalLight(0xffffff, 1);
// scene.add(directionalL);
// directionalL.position.set(60, 100, 40);
// directionalL.target = meshBox;
// // 开启投影
// directionalL.castShadow = true; // 允许该光源进行投影计算
// // 设置计算阴影的区域，最好刚好紧密包围在对象周围
// // 计算阴影的区域过大：模糊  过小：看不到或显示不完整
// directionalL.shadow.camera.near = .5;
// directionalL.shadow.camera.far = 300;
// directionalL.shadow.camera.left = -50;
// directionalL.shadow.camera.right = 50;
// directionalL.shadow.camera.top = 200;
// directionalL.shadow.camera.bottom = -100;
// directionalL.shadow.mapSize.set(1024,1024)

// 聚光光源
var spotLight = new THREE.SpotLight(0xffffff);
// 设置聚光光源位置
spotLight.position.set(50, 90, 50);
// 设置聚光光源发散角度
spotLight.angle = Math.PI / 6
scene.add(spotLight); //光对象添加到scene场景中
// 设置用于计算阴影的光源对象
spotLight.castShadow = true;
// 设置计算阴影的区域，注意包裹对象的周围
spotLight.shadow.camera.near = 1;
spotLight.shadow.camera.far = 300;
spotLight.shadow.camera.fov = 50;
spotLight.shadow.mapSize.set(1024,1024)


// 辅助轴
const axesH = new THREE.AxesHelper(200);
scene.add(axesH);

// 相机
const k = innerWidth / innerHeight,
      s = 200,
      camera = new THREE.OrthographicCamera(-s * k, s * k, s, -s, .5, 1000);
camera.position.set(400, 400, 400);
camera.lookAt(scene);

// 渲染器
const renderer = new THREE.WebGLRenderer();
renderer.setSize(innerWidth, innerHeight);
renderer.setClearColor(0x333333);
document.body.appendChild(renderer.domElement);

// 控制器
new OrbitControls(camera, renderer.domElement);

// 渲染函数
function render () {
  requestAnimationFrame(render);
  renderer.render(scene, camera);
}
render();